package com.ys.service.mail.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.mail.MailGroup;
import com.ys.entity.mail.MailGroupUser;
import com.ys.entity.sys.TsUserInfo;
import com.ys.mapper.mail.MailGroupMapper;
import com.ys.mapper.mail.MailGroupUserMapper;
import com.ys.service.mail.MailGroupService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;

@Service("mailGroupService")
public class MailGroupServiceImpl implements MailGroupService {
	@Autowired
	private MailGroupMapper mailGroupMapper;
	@Autowired
	private MailGroupUserMapper mailGroupUserMapper;

	@Override
	@Transactional
	public R add(String name) {
		
		if (name == null || name.length() == 0) {
			return R.error("组名不能为空");
		}
		
		MailGroup entity = null;
		{
			String userId = ShiroUtils.getUserId();
			
			entity = MailGroup.mailGroup(name, userId, 1);
			mailGroupMapper.insert(entity);
		}
		
		return R.ok();
	}

	@Override
	public R edit(Long id, String name) {
		
		MailGroup entity = new MailGroup().setId(id).queryFirst();

		entity.setName(name);
		mailGroupMapper.update(entity);
		
		return R.ok();
	}

	@Override
	public DtreeSqlDto dtree() {
		String loginUserId = ShiroUtils.getUserId();
		
		DtreeSqlDto.StatusDto status = DtreeSqlDto.StatusDto.init(200, "");

		List<DtreeSqlDto.DataDto> data = new ArrayList<DtreeSqlDto.DataDto>();
		List<MailGroup> listMailGroup = new MailGroup()
				.setStatus(1)
				.setUserId(loginUserId)
				.setOrderby("A.Id asc")
				.queryList();
		
		// 一级
		for(MailGroup mailGroup : listMailGroup) {
			Long groupId = mailGroup.getId();
			
			// 二级
			List<DtreeSqlDto.DataDto> children = new ArrayList<DtreeSqlDto.DataDto>();
			List<TsUserInfo> listTsUserInfo = new TsUserInfo()
					.addRightJoin("MAIL_GroupUser B on B.UserId = A.UserId")
					.where("B.GroupId = " + groupId)
					.queryList();
			for(TsUserInfo entity : listTsUserInfo) {
				
				String id = entity.getUserId();
				String title = entity.getUserId() + "/" + entity.getUserName();
				String parentId = mailGroup.getId().toString();
				
				DtreeSqlDto.DataDto dataDto = new DtreeSqlDto.DataDto(id, title, parentId, DtreeSqlDto.DataDto.initCheckArr(null), null);
				children.add(dataDto);
			}
			
			String id = groupId.toString();
			String title = mailGroup.getName();
			String parentId = "";
			
			DtreeSqlDto.DataDto dataDto = new DtreeSqlDto.DataDto(id, title, parentId, DtreeSqlDto.DataDto.initCheckArr(null), children);
			data.add(dataDto);
		}
		return new DtreeSqlDto(status, data);
	}

	@Override
	public R listUser(Long groupId) {
		if (groupId == null) {
			return R.error("组别ID不能为空");
		}
		List<MailGroupUser> listMailGroupUser = new MailGroupUser().setGroupId(groupId).queryList();
		List<ListUserDto.RowDto> rows = new ArrayList<ListUserDto.RowDto>();
		for(MailGroupUser entity : listMailGroupUser) {
			String userId = entity.getUserId();
			rows.add(new ListUserDto.RowDto(userId));
		}
		ListUserDto dto = new ListUserDto(rows);
		return R.ok(dto);
	}

	@Override
	public R addUsers(Long groupId, String userArr) {
		if (groupId == null) {
			return R.error("组别ID不能为空");
		}
		
		if (userArr == null || userArr.length() == 0) {
			return R.error("用户信息不能为空");
		}
		
		JSONArray userJA = JSON.parseArray(userArr);
		for (int i = 0; i < userJA.size(); i++) {
			JSONObject userJO = userJA.getJSONObject(i);
			String userId = userJO.getString("userId");
			
			MailGroupUser entity = new MailGroupUser().setGroupId(groupId).setUserId(userId).queryFirst();
			if (entity == null) {
				entity = MailGroupUser.mailGroupUser(groupId, userId, 1);
				mailGroupUserMapper.insert(entity);
			} else if(entity.getStatus() == 0) {
				entity.setStatus(1);
				mailGroupUserMapper.update(entity);
			}
		}
		return R.ok("操作成功");
	}
}
